package com.doit.day03

import scala.collection.mutable.ArrayBuffer


object _10_flatten算子 {
  def main(args: Array[String]): Unit = {

    //字符串本身就是一个字符数组拼接起来的
    val list: List[String] = List("hello", "qianjin", "zss")

    val res: List[Char] = list.flatten

    println(res)//List(h, e, l, l, o, q, i, a, n, j, i, n, z, s, s)


    val list1: List[Array[Int]] = List(Array(1, 2, 3, 4), Array(4, 5, 6, 7), Array(6, 7, 8, 9))

    //压平操作
    val res1: List[Int] = list1.flatten

    println(res1)//List(1, 2, 3, 4, 4, 5, 6, 7, 6, 7, 8, 9)

    def myFlatten(list:List[Array[Int]])={
      val buffer: ArrayBuffer[Int] = new ArrayBuffer[Int]()
      for (elem1 <- list) {
        for (elem2 <- elem1) {
          //这个elem2 就是数组中的一各个的元素
          buffer.append(elem2)
        }
      }
      buffer.toList
    }

    val ints: List[Int] = myFlatten(list1)
    println(ints)//List(1, 2, 3, 4, 4, 5, 6, 7, 6, 7, 8, 9)


    val arrs: List[Array[Array[Int]]] = List(Array(Array(1, 2, 3), Array(2, 3, 4)), Array(Array(5, 6, 7), Array(6, 7, 8)))
    val flatten: List[Array[Int]] = arrs.flatten
    val flatten1: List[Int] = flatten.flatten



    //需求： 从前往后推  从后往前推
    val lines: List[String] = List("hello,qianjin,where,is,your,girlFriends", "hello,wangzong,where,is,your,boyFriends")
    //flatmap这个算子==》flatten 和 map  算子 的结合
    println(lines.flatMap(_.split(",")))

    //我想得到一个这样的值
    //得到一个什么东西，可以变成这个样子
    /*
    List("hello,qianjin,where,is,your,girlFriends", "hello,wangzong,where,is,your,boyFriends")
    ==>List(Array(hello, qianjin, where, is, your, girlFriends),Array(hello, wangzong, where, is, your, boyFriends))
    */
    //==>怎么转  map(line=>line.split(","))
    //拿着句子去切割  line.split(",")
    //List(Array(单词，单词，单词),Array(单词，单词，单词)) .flatten
//    List("hello","qianjin","where","is"。。。。)

    val fun :(String)=> Array[String] = (line:String)=>{
      val arr: Array[String] = line.split(",")
      arr
    }

    val list2: List[Array[String]] = lines.map(fun)
    println(list2.flatten)

    val buf: ArrayBuffer[String] = new ArrayBuffer[String]()

    //能不能用算子搞定他呢？
    for (elem <- lines) {
      val arr: Array[String] = elem.split(",")
      for (elem <- arr) {
        buf.append(elem)
      }
    }
    println(buf.toList)













  }

}
